<template>
  <div class="container">
    <div class="panel panel-default">
      <div class="panel-heading">Todo List</div>
      <div class="panel-body">
        <Header :add-todo="addTodo" :todos="todos"/>
        <List :todos="todos" :del-todo="delTodo" />
        <Footer :todos="todos" :check-all="checkAll" :del-finished-task="delFinishedTask"/>
      </div>
    </div>
  </div>
</template>

<script>
import Header from './Header.vue'
import List from './List.vue'
import Footer from './Footer.vue'

export default {
  data() {
    return {
      todos: [
        {id: 1, title: '打一场羽毛球', finish: true},
        {id: 2, title: '打一场乒乓球', finish: false},
        {id: 3, title: '打一场篮球', finish: false},
      ]
    }
  },
  components: {
    Header,
    List,
    Footer
  },
  methods: {
    // 删除操作
    delTodo(id) {
      const index = this.todos.findIndex((item) => item.id === id)
      this.todos.splice(index, 1)
    },

    addTodo(item) {
      this.todos.unshift(item)
    },

    checkAll(flag) {
      this.todos.forEach((item) => {
        item.finish = flag
      })
    },

    delFinishedTask() {
      this.todos = this.todos.filter((item) => item.finish === false)
    }


  }
}
</script>

<style lang="scss" scoped>

</style>